<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<title>登录</title>
	<!-- 样 式 文 件 -->
	<link rel="stylesheet" href="component/pear/css/pear.css" />
	<link rel="stylesheet" href="admin/css/other/login.css" />
	<!-- 资 源 引 入 -->
	<script src="component/layui/layui.js"></script>
	<script src="component/pear/pear.js"></script>
</head>
<!-- 代 码 结 构 -->

<body background="admin/images/background.svg">
	<div class="bg">
		<form class="layui-form" action="javascript:void(0);" autocomplete="off">
			<div class="layui-form-item layui-logo">
				<img class="logo" src="/admin/images/logo.png" />
				<div class="title">BaseNet</div>
				<div class="desc">基于furion框架的中后台管理系统框架&copy;</div>
			</div>
			<div class="layui-form-item">
				<input type="text" name="account" class="layui-input" placeholder="请输入账号" lay-verify="account"
					maxlength="20" />
			</div>
			<div class="layui-form-item">
				<input type="password" name="password" class="layui-input" placeholder="请输入密码" lay-verify="password"
					maxlength="20" />
			</div>
			<div class="layui-form-item">
				<input type="text" name="code" class=" layui-input layui-input-inline" placeholder="验证码"
					lay-verify="required" maxlength="5" />
				<img src="admin/images/captcha.gif" class="codeImage" />
			</div>
			<div class="layui-form-item">
				<button type="submit" class="pear-btn pear-btn-success login" lay-submit lay-filter="login">
					登 录
				</button>
			</div>
		</form>
	</div>
</body>

</html>


<script type="text/javascript">
	layui.use(["jquery", "form", "button", "popup", "context"], function () {
		var $ = layui.jquery;
		var form = layui.form;
		var button = layui.button;
		var popup = layui.popup;
		var context = layui.context;

		var basePath = context.get("base-path");

		//表单提交验证
		form.verify({
			account: function (value, item) {
				if (value.length < 5) return '账号至少输入5位字符';
			},
			password: function (value) {
				if (value.length < 5) return '密码至少输入5位字符';
			}
		});
		//表单提交
		form.on('submit(login)', function (data) {
			var btn = button.load({ elem: '.login' })
			$.ajax({
				url: basePath + '/api/system/authorize/login',
				type: 'post',
				dataType: 'json',
				contentType: 'application/json',
				data: JSON.stringify(data.field),
				success: function (result) {
					if (result.succeeded) {
						popup.success("登录成功", function () {
							btn.stop();
							// 将 token 存储 localStore，传输格式如下
							//Authorization: Bearer 你的token
							//X-Authorization: Bearer 你的刷新token
							context.put("token", "Bearer " + result.data.accessToken);
							context.put("refreshToken", "Bearer " + result.data.refreshToken);
							location.href = "index.html";
						});
					} else {
						btn.stop();
						popup.failure(typeof (result.errors) == "string" ? result.errors : JSON.stringify(result.errors));
					}
				},
				error: function (xhr, status, error) {
					btn.stop();
					popup.failure("请求错误");
				}
			})
			return false;
		});
	})
</script>